package com.listview.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.listview.entity.Items;
import com.listview.util.DBHelper;

/**
 * 商品的业务逻辑类
 * 
 * @author jinmingwu
 *
 */
public class ItemsDAO {

	/**
	 * 显示所有的商品信息
	 * 
	 * @return
	 */
	public ArrayList<Items> getAllItems() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ArrayList<Items> list = new ArrayList<>(); // 商品集合

		try {
			conn = DBHelper.getConnection();
			String sql = "SELECT * FROM items;";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Items item = new Items();
				item.setId(rs.getInt("id"));
				item.setName(rs.getString("name"));
				item.setCity(rs.getString("city"));
				item.setPrice(rs.getInt("price"));
				item.setNumber(rs.getInt("number"));
				item.setPicture(rs.getString("picture"));
				list.add(item);
			}
			return list;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {

			// 释放数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 释放语句对象
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// 因单例模式，connection对象不释放
		}

	}

	/**
	 * 根据商品编号获取商品资料
	 * 
	 * @param id
	 * @return
	 */
	public Items getItemsById(int id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = DBHelper.getConnection();
			String sql = "SELECT * FROM items WHERE id=?;";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				Items item = new Items();
				item.setId(rs.getInt("id"));
				item.setName(rs.getString("name"));
				item.setCity(rs.getString("city"));
				item.setPrice(rs.getInt("price"));
				item.setNumber(rs.getInt("number"));
				item.setPicture(rs.getString("picture"));
				return item;
			} else {
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {

			// 释放数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 释放语句对象
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// 因单例模式，connection对象不释放
		}

	}

	/**
	 * 获取最近浏览的前五条商品信息
	 * 
	 * @param list
	 * @return
	 */
	public ArrayList<Items> getViewList(String list) {
		ArrayList<Items> itemsList = new ArrayList<>();
		int iCount = 5;// 计数，每次返回前5条记录
		if (list != null && list.length() > 0) {
			String[] arr = list.split(",");
			// if(arr.length >= 5) {
			// for(int i = arr.length - 1; i >= (arr.length - iCount); --i) {
			// itemsList.add(getItemsById(Integer.parseInt(arr[i])));
			// }
			// } else {
			// for(int i = arr.length - 1; i >= 0; --i) {
			// itemsList.add(getItemsById(Integer.parseInt(arr[i])));
			// }
			// }
			int lastNum = Integer.parseInt(arr[arr.length - 1]);
			itemsList.add(getItemsById(lastNum));
			for (int i = (arr.length - 2); (iCount > 1) && (i > 0); --i) {
				int nowNum = Integer.parseInt(arr[i]);
				if (lastNum != nowNum) {
					itemsList.add(getItemsById(nowNum));
					lastNum = nowNum;
					--iCount;
				}
			}
			return itemsList;
		} else {
			return null;
		}
	}
}
